|
|
|
|
|
|
|
'********************************
'Use of the DISTINCTROW predicate
'********************************
SELECT DISTINCTROW [Item Code] FROM [Retail Items], Sales _
[Retail Items] INNER JOIN Sales _
ON [Retail Items].[Item Code]=Sales.[Item Code] |
|
|
|
|
|
|
|
|
Setting Table Relationships |
|
|
|
|
|
|
|
|
When you design a database structure, you use key fields so that you can relate the tables in the database. For example, you use a salesperson ID in the Customers table to relate to the salesperson in the Salesperson table. You do this so that you don't have to include all the salesperson data with every customer record. You use these same key fields in the SELECT statement to set the table relationships so that you can display and manipulate the related data. That is, when you view customer information, you want to see the salesperson's name, not his or her ID. |
|
|
|
|
|
|
|
|
You can use two clauses to specify the relationships between tables: |
|
|
|
|
|
|
|
|
JOINThis combines two tables, based on the contents of specified fields in each table and the type of JOIN. |
|
|
|
|
|
|
|
|
WHEREThis usually is used to filter the records returned by a query, but it can be used to emulate an INNER JOIN. You will take a look at the INNER JOIN in the following section. |
|
|
|
|
|
|
|
|
|
Using the WHERE clause to join tables creates a read-only recordset. To create a modifiable recordset, you must use the JOIN clause.  |
|
|
|
|
|
|
|
|
|
|
The basic format of the JOIN clause is as follows: |
|
|
|
|
|
|
|
|
table1 {INNER|LEFT|RIGHT} JOIN table2 ON table1.key1 = table2.key2 |
|
|
|
|
|
|
|
|
The Query engine used by Visual Basic (also used by Access, Excel, and other Microsoft products) supports three JOIN clauses: INNER, LEFT, and RIGHT. Each of these clauses returns records that meet the JOIN condition, but each behaves differently in returning records that do not meet that condition. Table C.2 shows the records returned from each table for the three JOIN conditions. For this discussion, table1 is the left table and table2 is the right table. In general, the left table is the first one specified (on the left side of the JOIN keyword) and the right table is the second table specified (on the right side of the JOIN keyword). |
|
|
|
|
|
|
|
|
|
You can use any comparison operator (<, <=, =, >=, >, or <>) in the JOIN clause to relate the two tables.  |
|
|
|
|
|
|
|